﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace QAQ
{
    class GCPacket
    {
        private Stream mStream;
        private StreamReader mStreamReader;
        private StreamWriter mStreamWriter;
        public string mStringPasswd;
        public bool isEmpty;
        public int mLastId;
        private int mSetId;
        

        public GCPacket()
        {
            mStream = new MemoryStream(1024);
            mStreamReader = new StreamReader(mStream);
            mStreamWriter = new StreamWriter(mStream);
            mStringPasswd = "";
            isEmpty = true;
            mLastId = -1;
            mSetId = -1;
        }
        public int GetId()
        {
            if (true == isEmpty)
            {
                return mSetId;
            }
            mStreamReader.BaseStream.Seek(0, SeekOrigin.Begin);
            string recv=mStreamReader.ReadLine();
            int id=-2;
            try
            {
                id = Convert.ToInt32(recv);
                mLastId = id;
            }
            catch (Exception e)
            {
                //MessageBox.Show(recv);
                id = mLastId;
            }
            mStreamReader.BaseStream.SetLength(0);
            isEmpty = true;
            return id;
        }
        public int GetIdBlocking()
        {
            while (true == isEmpty) { }
            mStreamReader.BaseStream.Seek(0, SeekOrigin.Begin);
            string recv = mStreamReader.ReadLine();

            int id = -2;
            try
            {
                id = Convert.ToInt32(recv);
            }
            catch (Exception e)
            {
                //MessageBox.Show(recv);
            }
            mStreamReader.BaseStream.SetLength(0);
            isEmpty = true;
            return id;
        }        
        public void SetId(int sid)
        {            
            mStreamWriter.BaseStream.Seek(0, SeekOrigin.Begin);
            mStreamWriter.WriteLine(sid.ToString());
            mStreamWriter.Flush();
            if (sid != mLastId)
            {
                
            }
            mSetId = sid;
            mLastId = mSetId;
            isEmpty = false;
        }
        public void clear()
        {
            mLastId = -1;
            mStreamReader.BaseStream.SetLength(0);
            isEmpty = true;
            mStringPasswd = "";
        }
        public bool isValid()
        {
            if (this.mSetId <= 0)
            {
                return false;
            }
            return GenerateKey().CompareTo(this.mStringPasswd) == 0;
        }
        private string GenerateKey()
        {
            return GenerateKey(this.mSetId);
        }
        public string GenerateKey(int salt)
        {
            Random r = new Random(salt);
            int chk = r.Next(0, 32767);
            int pwlen = 15;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < pwlen; ++i)
            {
                if (((chk >> i) & (0x1)) == 0x1)
                {
                    sb.Append('0');
                }
                else
                {
                    sb.Append('.');
                }
            }
            return sb.ToString();
        }

    }
}
